package com.szmmsoft.mp.service.impl;

import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szmmsoft.mp.common.constant.CacheConstants;
import com.szmmsoft.mp.common.exception.BusinessException;
import com.szmmsoft.mp.common.model.R;
import com.szmmsoft.mp.common.until.JWTUtil;
import com.szmmsoft.mp.common.until.RedisUtils;
import com.szmmsoft.mp.model.dto.AccountLoginDTO;
import com.szmmsoft.mp.model.entity.User;
import com.szmmsoft.mp.model.vo.LoginVO;
import com.szmmsoft.mp.service.UserService;
import com.szmmsoft.mp.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
* @author ethan
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-10-16 13:57:43
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Override
    public R<LoginVO> accountLogin(AccountLoginDTO accountLoginDTO) {
        User user = lambdaQuery()
                .eq(User::getUserName,accountLoginDTO.getName())
                .one();
        if (user==null){
            throw new BusinessException("用户名不存在");
        }
        if (!user.getPassword().equals(accountLoginDTO.getPwd())){
            throw new BusinessException("密码不正确");
        }
        String token = JWTUtil.token(user.getId());
        RedisUtils.set(String.format(CacheConstants.LOGIN_USER,user.getId()),token, Duration.ofMillis(JWTUtil.JWT_EXPIRE_TIME));
        return R.ok(LoginVO.builder().token(token).build());
    }

    @Override
    public LoginVO wxLogin(String code) {
//        boolean isNew = false;
//        boolean haveWxOpenId = true;
//        JshSupplier user = null;
//        WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(code);
//        log.info("phoneNoInfo:" + phoneNoInfo.getPhoneNumber());
//        JshSupplier portalUser = jshSupplierService.getOne(new LambdaQueryWrapper<JshSupplier>()
//                .eq(JshSupplier::getPhoneNum, phoneNoInfo.getPhoneNumber()));
//        if (!StringUtils.isEmpty(invitationCode) && invitationCode.length() > 3) {
//            user = jshSupplierService.getOne(new LambdaQueryWrapper<JshSupplier>()
//                    .eq(JshSupplier::getInvitationCode, invitationCode));
//        }
//        if (portalUser == null) {
//            //注册用户
//            isNew = true;
//            portalUser = new JshSupplier();
//            portalUser.setSupplier("");
//            portalUser.setCreateTime(new Date());
//            portalUser.setTenantId(63L);
//            portalUser.setEnabled(true);
//            portalUser.setType(JshSupplierEnum.MEMBER.getInfo());
//            portalUser.setTelephone(phoneNoInfo.getPhoneNumber());
//            portalUser.setPhoneNum(phoneNoInfo.getPhoneNumber());
//            portalUser.setParentId(user != null ? user.getId() : null);
//        } else {
//            if (StringUtils.isEmpty(portalUser.getWxOpenId()) && portalUser.getWxOpenId().length() < 2) {
//                haveWxOpenId = false;
//            }
//        }
//
//        // 更新登录日志
//        if (isNew) {
//            jshSupplierService.save(portalUser);
//            log.info("新用户注册成功：{}", portalUser);
//            // TODO: 发送新用户优惠券 2024/3/28
//        } else {
//            if (portalUser.getParentId() == null && user != null) {
//                portalUser.setParentId(user.getId());
//            }
//            jshSupplierService.updateById(portalUser);
//        }
//        LoginVO vo = new LoginVO();
//        Map<String, Object> map = new HashMap<>(16);
//        map.put("id", portalUser.getId());
//        vo.setNew(isNew);
//        vo.setHaveWxOpenId(haveWxOpenId);
//
//        StpUtil.login(portalUser.getId());
//        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
//        vo.setToken(tokenInfo.getTokenValue());
        return LoginVO.builder().build();
    }
}
